/*
 * FlickrJ.java
 *
 * Created on August 27, 2006, 6:26 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package flickit;

import java.io.IOException;
import java.util.*;
import com.aetrion.flickr.*;
import com.aetrion.flickr.photos.*;
import com.aetrion.flickr.photosets.*;
import com.aetrion.flickr.people.*;
import org.xml.sax.SAXException;

/**
 *
 * @author afung
 */
public class FlickrJ {
    
    Flickr flickr;
    PhotosInterface photosiface;
    
    // Array of photos from a set.  Used in search for duplicates.
    ArrayList<PhotoInfo> photosinset;
    
    
    /** Creates a new instance of FlickrJ */
    public FlickrJ( String apikey ) {
        flickr = new Flickr( apikey );
        photosiface = flickr.getPhotosInterface();
    }
    
    
    public String getUserId ( String username )
    {
        PeopleInterface piface = flickr.getPeopleInterface();
        User user;
        try {
            user = piface.findByUsername(username);
            return user.getId();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        } catch (FlickrException ex) {
            ex.printStackTrace();
        }
        return "";
    }
    
    public void listUsersPhotosets ( String userid )
    {
        PhotosetsInterface setiface = flickr.getPhotosetsInterface();
        
        try {
            Photosets photosets = setiface.getList(userid);
            Collection sets = photosets.getPhotosets();
            Iterator it = sets.iterator();
            while ( it.hasNext() ) {
                Photoset pset = (Photoset) it.next();
                
                System.out.print ( pset.getId() + " - " );
                System.out.println ( pset.getTitle() );
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (FlickrException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        }
    
    }
    
    /*
     * For all photos in a set, fill out photosinset collection
     */
    public void listPhotosByPhotosetId ( String photosetid )
    {
        photosinset = new ArrayList<PhotoInfo>();
        
        PhotosetsInterface setiface = flickr.getPhotosetsInterface();
        
        try {
            Collection photos = setiface.getPhotos( photosetid );
            Iterator it = photos.iterator();
            while ( it.hasNext() ) {
                Photo photo = (Photo) it.next();
                photosinset.add( getPhotoInfo ( photo.getId() ) );
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (FlickrException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        }
    }
    
    public PhotoInfo getPhotoInfo ( String photoid )
    {
        PhotoInfo newphoto = null;
        
        try {
            Photo photo = photosiface.getInfo( photoid, "" );
            Size origsize = getOrigSize( photoid );
            
            if ( photo.getTitle().equals("DSC_0355.jpg") ) {
                // file name
                System.out.println ( photo.getTitle() );
                // file size    
                System.out.println( "Size: " + origsize.getWidth() + " x " + origsize.getHeight() );
            }
                
            newphoto = new PhotoInfo();
            newphoto.id = photoid;
            newphoto.filename = photo.getTitle();
            newphoto.height = origsize.getHeight();
            newphoto.width = origsize.getWidth();
            
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (FlickrException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        }
        
        return newphoto;
    }
    
    /*
     * Use filename, width, height to check if a photo already exists
     * in the set
     */
    public boolean isPhotoInSet( String filename, int width, int height )
    {
       Iterator it = photosinset.iterator();
       for ( PhotoInfo photo : photosinset ) {
           if ( photo.filename.equals( filename ) ) {
               if ( photo.height == height ) {
                   if ( photo.width == width ) {
                       return true;
                   }
               }
           }
       }
       return false;
    }
    
    protected Size getOrigSize ( String photoid )
    {
        Size biggest = null;
        
        try {
            Collection sizes = photosiface.getSizes( photoid );
            Iterator it = sizes.iterator();
            while ( it.hasNext() ) {
                Size cursize = (Size) it.next();
                if ( biggest == null ) {
                    biggest = cursize;
                }
                else {
                    // compare size with biggest
                    if ( cursize.getHeight() > biggest.getHeight() ) {
                        biggest = cursize;
                    }
                }
            }
            
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (FlickrException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        }
        
        return biggest;
        
    }
    
    public class PhotoInfo
    {
        String id;
        String filename;
        int height;
        int width;
    }
}
